package edu.gatech.cs2340.aperturescience;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import edu.gatech.cs2340.aperturescience.backend.task.Task;
import edu.gatech.cs2340.aperturescience.backend.task.TasksDataSource;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

/**
 * TaskListAdapter oversees the mingling between TaskViewActivity and its respective xml file
 * 
 * @author Aperture Science
 * @3/26/12
 *
 */
public class TaskListAdapter extends ArrayAdapter<Task>{
	Context context; 
    int layoutResourceId;    
    Task[] data = null;
    TasksDataSource dataSource = null;
    
    public TaskListAdapter(TasksDataSource dataSource, Context context, int layoutResourceId, Task[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.dataSource = dataSource; 
        this.data = data;
    }

    /**
     * View returns the appropriate view
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        TaskListData listData= null;
        final Task task = data[position];
        if(row == null)
        {
        	
            LayoutInflater inflater = ((android.app.Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            listData = new TaskListData();
            listData.name = (TextView)row.findViewById(R.id.task_list_item_top);
            listData.description = (TextView)row.findViewById(R.id.task_list_item_bottom);
            listData.date = (TextView)row.findViewById(R.id.task_list_date_top);
            listData.time = (TextView)row.findViewById(R.id.task_list_date_bottom);
            listData.completed = (CheckBox)row.findViewById(R.id.completedCheckbox);
            listData.completed.setOnCheckedChangeListener(new OnCheckedChangeListener()
            {

				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean checked) {
					task.completed = checked;
					dataSource.setCompleted(task.uniqueID, checked);
				}
            	
            });
            row.setTag(listData);
        }
        else
        {
        	listData = (TaskListData) row.getTag();
        }
        SimpleDateFormat date = new SimpleDateFormat("MMM d");
        DateFormat time = android.text.format.DateFormat.getTimeFormat(context);
        listData.name.setText(task.name);
        listData.description.setText(task.description);
        listData.date.setText(date.format(task.dueDate));
        listData.time.setText(time.format(task.dueDate));
        if(task.completed)
        	listData.completed.setChecked(true);
        return row;
    }
    static class TaskListData {
    	TextView name;
    	TextView description;
    	TextView date;
    	TextView time;
    	CheckBox completed;
    }
}
